{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter = Counter(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({1: 1, 2: 1, 3: 1})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 7, 6, 5, 2, 1, 1]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candidates = [10,1,2,7,6,1,5]; target = 8\n",
    "candidates.sort(reverse=True)\n",
    "candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 6, 5, 2, 1, 1]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(candidates)):\n",
    "    if candidates[i] <= target:\n",
    "        candidates = candidates[i:]\n",
    "        break\n",
    "candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import combinations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1,),\n",
       " (1, 1),\n",
       " (2,),\n",
       " (2, 1),\n",
       " (2, 1, 1),\n",
       " (5,),\n",
       " (5, 1),\n",
       " (5, 1, 1),\n",
       " (5, 2),\n",
       " (5, 2, 1),\n",
       " (5, 2, 1, 1),\n",
       " (6,),\n",
       " (6, 1),\n",
       " (6, 1, 1),\n",
       " (6, 2),\n",
       " (6, 2, 1),\n",
       " (6, 2, 1, 1),\n",
       " (6, 5),\n",
       " (6, 5, 1),\n",
       " (6, 5, 1, 1),\n",
       " (6, 5, 2),\n",
       " (6, 5, 2, 1),\n",
       " (6, 5, 2, 1, 1),\n",
       " (7,),\n",
       " (7, 1),\n",
       " (7, 1, 1),\n",
       " (7, 2),\n",
       " (7, 2, 1),\n",
       " (7, 2, 1, 1),\n",
       " (7, 5),\n",
       " (7, 5, 1),\n",
       " (7, 5, 1, 1),\n",
       " (7, 5, 2),\n",
       " (7, 5, 2, 1),\n",
       " (7, 5, 2, 1, 1),\n",
       " (7, 6),\n",
       " (7, 6, 1),\n",
       " (7, 6, 1, 1),\n",
       " (7, 6, 2),\n",
       " (7, 6, 2, 1),\n",
       " (7, 6, 2, 1, 1),\n",
       " (7, 6, 5),\n",
       " (7, 6, 5, 1),\n",
       " (7, 6, 5, 1, 1),\n",
       " (7, 6, 5, 2),\n",
       " (7, 6, 5, 2, 1),\n",
       " (7, 6, 5, 2, 1, 1)}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inputs = set()\n",
    "for i in range(1, len(candidates)+1):\n",
    "    inputs.update(set(combinations(candidates, i)))\n",
    "inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 5], [2, 6], [1, 7], [1, 1, 6]]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "for l in inputs:\n",
    "    if sum(l) == target:\n",
    "        l = list(l)\n",
    "        l.sort()\n",
    "        results.append(l)\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Time Out\n",
    "\n",
    "\n",
    "```python\n",
    "from itertools import combinations\n",
    "\n",
    "class Solution:\n",
    "    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:\n",
    "        candidates.sort(reverse=True)\n",
    "        for i in range(len(candidates)):\n",
    "            if candidates[i] <= target:\n",
    "                candidates = candidates[i:]\n",
    "                break\n",
    "        results = []\n",
    "        for i in range(1, len(candidates)+1):\n",
    "            for l in combinations(candidates, i):\n",
    "                if sum(l) == target:\n",
    "                    l = list(l)\n",
    "                    l.sort()\n",
    "                    if l not in results:\n",
    "                        results.append(l) \n",
    "        return results\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "candidates = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 2]\n",
      "[1, 2, 3]\n",
      "[1, 2, 3, 4]\n",
      "[2]\n",
      "[2, 3]\n",
      "[2, 3, 4]\n",
      "[3]\n",
      "[3, 4]\n",
      "[4]\n"
     ]
    }
   ],
   "source": [
    "inputs = []\n",
    "for i in range(len(candidates)):\n",
    "    for j in range(1, len(candidates)-i + 1):\n",
    "        print(candidates[i:i+j])\n",
    "        inputs.append(candidates[i:i+j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 5, 6, 7, 10]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candidates = [10,1,2,7,6,1,5]; target = 8\n",
    "candidates.sort()\n",
    "candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 5, 6, 7, 10]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(candidates)):\n",
    "    if candidates[i] <= target:\n",
    "        candidates = candidates[i:]\n",
    "        break\n",
    "candidates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5040"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from itertools import permutations\n",
    "\n",
    "len(list(permutations(candidates, len(candidates))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 1]\n",
      "[1, 1, 2]\n",
      "[1, 1, 2, 5]\n",
      "[1, 1, 2, 5, 6]\n",
      "[1, 1, 2, 5, 6, 7]\n",
      "[1, 1, 2, 5, 6, 7, 10]\n",
      "[1]\n",
      "[1, 2]\n",
      "[1, 2, 5]\n",
      "[1, 2, 5, 6]\n",
      "[1, 2, 5, 6, 7]\n",
      "[1, 2, 5, 6, 7, 10]\n",
      "[2]\n",
      "[2, 5]\n",
      "[2, 5, 6]\n",
      "[2, 5, 6, 7]\n",
      "[2, 5, 6, 7, 10]\n",
      "[5]\n",
      "[5, 6]\n",
      "[5, 6, 7]\n",
      "[5, 6, 7, 10]\n",
      "[6]\n",
      "[6, 7]\n",
      "[6, 7, 10]\n",
      "[7]\n",
      "[7, 10]\n",
      "[10]\n"
     ]
    }
   ],
   "source": [
    "inputs = []\n",
    "for i in range(len(candidates)):\n",
    "    for j in range(1, len(candidates)-i + 1):\n",
    "        print(candidates[i:i+j])\n",
    "        inputs.append(candidates[i:i+j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 5]]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "for l in inputs:\n",
    "    if sum(l) == target:\n",
    "        results.append(l)\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/combination-sum-ii\n",
    "\n",
    "\n",
    "\n",
    "Time Limit Exceeded\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "from itertools import combinations\n",
    "\n",
    "class Solution:\n",
    "    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:\n",
    "        candidates.sort(reverse=True)\n",
    "        for i in range(len(candidates)):\n",
    "            if candidates[i] <= target:\n",
    "                candidates = candidates[i:]\n",
    "                break\n",
    "                \n",
    "        inputs = set()\n",
    "        for i in range(1, len(candidates)+1):\n",
    "            inputs.update(set(combinations(candidates, i)))\n",
    "            \n",
    "        results = []\n",
    "        for l in inputs:\n",
    "            if sum(l) == target:\n",
    "                l = list(l)\n",
    "                l.sort()\n",
    "                results.append(l)\n",
    "        return results\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/combination-sum-ii\n",
    "\n",
    "\n",
    "Runtime: 432 ms, faster than 5.45% of Python3 online submissions for Combination Sum II.\n",
    "Memory Usage: 20 MB, less than 5.24% of Python3 online submissions for Combination Sum II.\n",
    "\n",
    "\n",
    "```python\n",
    "from itertools import combinations\n",
    "\n",
    "class Solution:\n",
    "    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:\n",
    "        #8:16\n",
    "        \"\"\"\n",
    "        result = []\n",
    "        for i in range(0, len(candidates)):\n",
    "            #print(i)\n",
    "            possible_combinations = combinations(candidates, i+1)\n",
    "            for one in possible_combinations:\n",
    "                s = sum(one)\n",
    "                if s == target:\n",
    "                    one = list(one)\n",
    "                    one.sort()\n",
    "                    if one not in result:\n",
    "                        result.append(one)\n",
    "        return result\n",
    "        #8:20 to make a timeout\n",
    "        \"\"\"\n",
    "        if sum(candidates) < target:\n",
    "            return []\n",
    "        \n",
    "        candidates.sort()\n",
    "        result = []\n",
    "        def go_through(this_list, list_left):\n",
    "            if (len(list_left) == 0 or len(this_list) == len(candidates)):\n",
    "                return\n",
    "            else:\n",
    "                for num in list_left.copy():\n",
    "                    new_list = this_list + [num]\n",
    "                    s = sum(new_list)\n",
    "                    if s > target:\n",
    "                        return\n",
    "                    elif s == target:\n",
    "                        new_list.sort()\n",
    "                        result.append(new_list)\n",
    "                        #return\n",
    "                    else:\n",
    "                        l = list_left.copy()\n",
    "                        l.remove(num)\n",
    "                        go_through(new_list, l)\n",
    "        go_through([], candidates)\n",
    "        \n",
    "        new_result = []\n",
    "        for l in result:\n",
    "            if l not in new_result:\n",
    "                new_result.append(l)\n",
    "            \n",
    "        return new_result\n",
    "        #8:50\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
